package com.ehotting.mqbd.core.service;

import com.ehotting.eaf.redis.service.RedisService;
import com.ehotting.mqbd.api.MQDBConstants;
import com.ehotting.mqbd.api.bean.solver.SolverInfoBean;
import com.ehotting.mqbd.api.bean.solver.SolverStateBean;
import com.ehotting.mqbd.core.manager.SolverInfoManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.TimeUnit;

@Component
public class SolverInfoService {

    private Logger logger = LoggerFactory.getLogger(SolverInfoService.class);

    @Autowired
    private RedisService redisService;

    public Map<String, List<SolverStateBean>> getMapServerInfoList(){

        Collection<String> keys = redisService.keys(MQDBConstants.SOLVER_INFO_KEY + "*");
        Map<String, List<SolverStateBean>> map = new HashMap<String, List<SolverStateBean>>();
        for(String key :keys){
            //log.info("key:{}",key);
            SolverStateBean solverStateBean = redisService.getCacheObject(key);
            String[] keyArray = key.split(MQDBConstants.COLO);
            /**MQDB:SOLVER_INFO_KEY:{module}**/
            String module = keyArray[2];
            if(map.containsKey(module)){
                map.get(module).add(solverStateBean);
            }else{
                List<SolverStateBean> list = new ArrayList<>();
                list.add(solverStateBean);
                map.put(module,list);
            }
        }

        return map;
    }




    public List<SolverStateBean> getByModule(String module){
        Collection<String> keys = redisService.keys(MQDBConstants.SOLVER_INFO_KEY +module+ MQDBConstants.COLO +"*");
        List<SolverStateBean> solverStateList = new ArrayList<>();
        for(String key :keys ){
            solverStateList.add(redisService.getCacheObject(key));
        }
        return solverStateList;
    }



    public SolverStateBean get(String module, String ip){
        SolverStateBean solverState = redisService.getCacheObject(MQDBConstants.SOLVER_INFO_KEY +module+MQDBConstants.COLO +ip);
        return solverState;
    }



    public void delete(String module, String ip){
        redisService.delete(MQDBConstants.SOLVER_INFO_KEY +module+MQDBConstants.COLO +ip);
    }


    public void batchSave(List<SolverStateBean> solverStateList){
        for(SolverStateBean solverState : solverStateList){
            if(solverState.getModule()==null){
                logger.error("module is null,return.");
                continue;
            }
            redisService.setCacheObject(MQDBConstants.SOLVER_INFO_KEY +solverState.getModule()+MQDBConstants.COLO +solverState.getSolverInfo().getIp(),solverState);
        }
    }

    public void save(SolverStateBean solverState){
        if(solverState.getModule()==null){
            logger.error("module is null,return.");
            return;
        }
        redisService.setCacheObject(MQDBConstants.SOLVER_INFO_KEY +solverState.getModule()+MQDBConstants.COLO +solverState.getSolverInfo().getIp(),solverState);
    }




}
